home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Software Vault: The Gold Collection
/
Software Vault - The Gold Collection (American Databankers) (1993).ISO
/
cdr05
/
xnot12a.zip
/
DIR.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-20
|
3KB
|
191 lines
#include "jam.h"
/*
* Name: MG 2a
* Directory management functions
* Created: Ron Flax (ron@vsedev.vse.com)
* Modified for MG 2a by Mic Kaczmarczik 03-Aug-1987
*/
#include "def.h"
#include "keyname.h"
#ifdef FAT
# include "direct.h"
#else
# include "dirent.h"
# include "unistd.h"
#endif
#include "malloc.h"
#ifndef NO_DIR
#ifndef getwd /* may be a #define */
char *getwd();
#endif
char *wdir = 0;
static char cwd[NFILEN];
static char *nocwd = "Can't get current directory, trying HOME!";
static char *nohome = "Can't resolve HOME!";
#ifndef WINDOWED
void setdir(s)
char *s;
{
chdir(s);
}
#endif
/*
* Initialize anything the directory management routines need
*/
dirinit()
{
char savename[MAXPATH];
savename[0] = '\0';
if (cwd[0])
strcpy(savename, cwd); /* have old valid dir? */
else
strcpy(savename, homedir); /* revert to HOME if can */
wdir = getwd(cwd);
if (!wdir)
{
ewprintf(nocwd);
if (savename[0] != '\0')
{
strcpy(cwd, savename);
wdir = cwd;
}
else
panic(nohome);
}
return TRUE;
}
/* split from changedir (JAM)
*/
gotodir(bufc)
char *bufc;
{
char savename[MAXPATH];
char *temp = (char *)calloc(strlen(bufc) + 1, sizeof(char));
char *temp1 = (char *)calloc(strlen(wdir) + 1, sizeof(char));
BOOL s = FALSE;
if (temp && temp1)
{
strcpy(temp, bufc);
strcpy(temp1, wdir);
adjustnamecase(temp);
adjustnamecase(temp1);
s = (strcmp(temp, temp1) == 0);
}
free(temp);
free(temp1);
if (s)
return(TRUE); /* already same place */
strcpy(savename, cwd); /* save old dir in case error */
#ifdef FAT
if (bufc[1] && (bufc[1] == ':')) /* drive spec */
setdisk(bufc[0]);
#endif
if (chdir(bufc) == -1)
{
ewprintf("Can't change dir to %s", bufc);
return(FALSE);
}
else
{
if (!(wdir = getwd(cwd)))
{
ewprintf(nocwd);
strcpy(cwd, savename);
wdir = cwd;
return (FALSE);
}
return(TRUE);
}
}
/* given a BUFFER, make the file's root directory
* the current directory. makes it easy to support
* visit-file, etc, to same dir
*/
#include "kbd.h" /* sigh */
switchdir(bp)
BUFFER *bp;
{
char buf[NFILEN];
BOOL result = FALSE;
if (bp->b_fname[0] && ((bp->b_flag & BFDIR) == 0))
{
dirfrombp(buf, bp);
result = gotodir(buf);
if (!result)
bp->b_flag |= BFDIR; /* failure, don't try again */
}
return result;
}
void dirfrombp(buf, bp)
char *buf;
BUFFER *bp;
{
extern MAPS *name_mode();
register char *s;
strcpy(buf, bp->b_fname); /* whole name */
/* strip name from normal 'file' buffers
*/
s = &buf[strlen(buf)-1];
if (bp->b_modes[0] != name_mode(DiredStr))
for (; (*s && (s >= buf) && (*s != BDC1)); s--)
;
if ((s >= buf) && (*s == BDC1))
*s = '\0';
/* root check, ick
*/
for (s = buf; *s; s++)
if (*(s + 1) == '\0')
if (*s == ':')
{
*(s + 1) = BDC1;
*(s + 2) = '\0';
break;
}
if (strcmp(buf, bp->b_fname) == 0) /* not computed, use cwd */
strcpy(buf, cwd);
}
char *dirpath()
{
static char buf[NFILEN+1];
register int i;
strcpy(buf, wdir);
i = strlen(buf) - 1;
if (buf[i] != BDC1)
{
i++;
buf[i++] = BDC1;
buf[i] = '\0';
}
adjustnamecase(buf);
return(buf);
}
#endif